package com.xdclass.mobile.mobilexdclassredis.service;

import com.xdclass.mobile.mobilexdclassredis.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created whit IntelliJ IDEA
 * User:杨骏杰
 * Date:2021/3/31
 * Time:13:28
 */
@Service
public class SeckillService {

    //redis开始时间标识符
    private static final String secStartPrefix = "skuId_start_";
    //redis秒杀的拦截
    private static final String secAccess = "skuId_access_";
    //redis秒杀物品的库存
    private static final String secCount = "skuId_count_";
    private static final String filterName = "skuId_bloomFilter_";
    private static final String bookedName = "skuId_booked_";


    @Resource
    private RedisService redisService;

    public String seckill(int uid,int skuId){
        //流量拦截层
        //1，判断秒杀是否开始 0_1554045087   开始标识_开始时间
        String isStart = (String) redisService.get(secStartPrefix+skuId);
        if(StringUtils.isBlank(isStart)){
            return "还未开始";
        }
        if(isStart.contains("_")) {
            Integer isStartInt = Integer.valueOf(isStart.split("_")[0]);
            //开始时间
            Integer startTime = Integer.valueOf(isStart.split("_")[1]);
            if(isStartInt==0){
                //如果start大于当前时间，表示未开始
                if(startTime>getNow()){
                    return "还未开始";
                }else {
                    //代表秒杀已经开始
                    redisService.set(secStartPrefix+skuId,1+"");
                }
            }else {
                return "系统异常";
            }
        }else {
            if(Integer.parseInt(isStart)!=1){
                //则是系统异常
                return "系统异常";
            }
        }
        //流量拦截
        String skuIdAccessName = secAccess + skuId;
        Integer accessNumInt = 0;
        String accessNum = (String) redisService.get(skuIdAccessName);
        if(StringUtils.isNotBlank(accessNum)){
            accessNumInt = Integer.parseInt(accessNum);
        }
        String skuIdCountName = secCount + skuId;
        Integer countNumInt = Integer.parseInt((String) redisService.get(skuIdCountName));
        System.out.println("countNumInt:"+countNumInt*1.2+"==accessNumInt:"+accessNumInt);
        if (countNumInt * 1.2 < accessNumInt) {
            return "抢购已经完成,欢迎下次参与";
        }
        //信息校验层
        if (redisService.bloomFilterExists(filterName, uid)){
            return "您已经抢购过该商品，请勿重复下发!";
        }else{
            redisService.incr(skuIdAccessName);
            redisService.bloomFilterAdd(filterName, uid);
        }
        Boolean isSuccess = redisService.getAndIncrLua(bookedName+skuId);
        if(isSuccess){
            return "恭喜您抢购成功！！！";
        }else{
            return "抢购结束,欢迎下次参与";
        }
    }

    private long getNow(){
        return System.currentTimeMillis()/1000;
    }
}